package chpt05.shopping.ui;

import java.util.List;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import chpt05.shopping.Commodity;
import chpt05.shopping.ShoppingCart;
import chpt05.shopping.ShoppingCart.CKResult;
import chpt05.shopping.ShoppingCart.ItemEntry;


public class GUIMenuImpl implements IMenu {

	@Override
	public void showShoppingCart(ShoppingCart cart) {
		StringBuilder sb = new StringBuilder();
		String title = String.format("***购物车清单***%n");
		String header = String.format("%s\t%-10s\t%-10s\t%-5s\t%-10s%n",
				"编号", "商品", "详情", "数量", "单价");
		String footer = String.format("**************%n");
		sb.append(title);
		sb.append(header);
		// 商品信息
		ShoppingCart.ItemEntry[] items = cart.getItems();
		for (int i = 0; i < items.length; i++) {
			ItemEntry item = items[i];
			Commodity c = item.getItem();
			String x = String.format("%s\t%-10s\t%-10s\t%-5s\t%-5.2f%n", 
					i + 1, c.getName(), c.getDescription(), 
					item.getQty(),c.getPrice());
			sb.append(x);
		}
		// 结算信息
		CKResult r = cart.checkout();
		sb.append(String.format("商品数量：%s%n", r.getQty()));
		sb.append(String.format("商品总价：%s%n", r.getPrice()));
		sb.append(footer);
		//System.out.println(sb);
		
		JDialog dialog = new JDialog();
		JTextArea textArea = new JTextArea();
		textArea.setFont(new java.awt.Font("黑体", 1, 16));
		dialog.add(textArea);
		textArea.append(sb.toString());
		dialog.pack(); // 让dialog自动调整到尺寸
		dialog.setVisible(true);
	}

}
